﻿using LyyCMS.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LyyCMS.Web
{
    #region MySQL 失败
    //public class LyyCMSDbContextFactory : IDesignTimeDbContextFactory<MySqlEntities>
    //{
    //    public MySqlEntities CreateDbContext(string[] args)
    //    {
    //        string appSetttingBasePath = Directory.GetCurrentDirectory();
    //        var configBuilder = new ConfigurationBuilder()
    //            .SetBasePath(appSetttingBasePath)
    //            .AddJsonFile("appsettings.json")
    //            .Build();

    //        var builder = new DbContextOptionsBuilder<MySqlEntities>();
    //        builder.UseSqlServer(configBuilder.GetConnectionString("MySqlDatabase"), b => b.MigrationsAssembly("LyyCMS.Web"));
    //        return new MySqlEntities(builder.Options);
    //    }
    //}
    #endregion

    // MSSQL
    public class LyyCMSDbContextFactory : IDesignTimeDbContextFactory<LyyCMSEntities>
    {
        public LyyCMSEntities CreateDbContext(string[] args)
        {
            //Directory.SetCurrentDirectory(".."); //设置当前路径 为当前解决方案的路径

            string appSetttingBasePath = Directory.GetCurrentDirectory();
            var configBuilder = new ConfigurationBuilder()
                .SetBasePath(appSetttingBasePath)
                .AddJsonFile("appsettings.json")
                .Build();

            //var connString = "Server=.;Database=LyyCMS;Trusted_Connection=True;";

            var builder = new DbContextOptionsBuilder<LyyCMSEntities>();
            builder.UseSqlServer(configBuilder.GetConnectionString("BloggingDatabase"), b => b.MigrationsAssembly("LyyCMS.Web"));
            return new LyyCMSEntities(builder.Options);
        }
    }
}
